package wo.resume.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import wo.resume.dto.WorkDto;
import wo.resume.service.WorkService;
import wo.bsys.vo.WoDataTable;
import wo.bsys.vo.WoSelectorParams;
import wo.common.entity.WoPage;
import wo.common.exception.WoResultCode;
import wo.common.util.WoJsonUtil;
import wo.department.dto.StaffDto;
import wo.bsys.util.BSysConstant;

/**
 * Work控制器.
 * @author cailei
 */
@Controller
@RequestMapping("/resume/work")
@SessionAttributes(names = BSysConstant.SESSION_USER)
public class WorkController {

	/**
	 * 注入WorkService.
	 */
    @Resource // @Autowired
	private WorkService workService;
	
	/**
	 * @param draw DataTable控件请求序列号,返回数据时需要设置该值
	 * @param start 当前页开始索引,从0开始
	 * @param length 当前页最大行数
	 * @param searchContent 查询内容
	 * @param dir 排序方式
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public WoDataTable<WorkDto> getDataTable(Integer draw, Long start, Long length,
			@RequestParam("search[value]") String searchContent, @RequestParam("order[0][dir]") String dir
                        ,java.lang.String resumeId
            			) {
		WoPage<WorkDto> page = workService.getPageData(start, length, searchContent, dir
		        ,resumeId
        		);
		return new WoDataTable<WorkDto>(page, draw);
	}
	
	/**
	 * 加载"创建"页面
	 * @return
	 */
	@GetMapping("/create")
	public String create (Map<String, Object> map
		        ,java.lang.String resumeId
        		) {
		map.put("url", "work/create.jsp");
		
				// 设置M21关联对象id
		WorkDto dto = new WorkDto();
		dto.setResumeId(resumeId);
		map.put("formData", dto);
				return "main";
	}
	
	/**
	 * 提交"创建"表单
	 * @return
	 */
	@PostMapping("/create")
	public String create (WorkDto dto, Map<String, Object> map) {
		workService.create(dto);
				return "redirect:/resume/resume/update?id=" + dto.getResumeId();
			}
	
	/**
	 * 加载"修改"页面
	 * @return
	 */
	@GetMapping("/update")
	public String update (String id, Map<String, Object> map) {
		WorkDto dto = workService.getById (id);
		map.put("url", "work/update.jsp");
		map.put("formData", dto);
		return "main";
	}
	
	/**
	 * 提交"修改"表单
	 * @return
	 */
	@PostMapping("/update")
	public String update (WorkDto dto, Map<String, Object> map) {
		workService.update(dto);
				return "redirect:/resume/resume/update?id=" + dto.getResumeId();
			}
	
	/**
	 * @param id
	 * @return
	 */
	@PostMapping ("/delete")
	@ResponseBody
	public WoResultCode delete (String[] id) {
		workService.delete(id);
		return WoResultCode.getSuccessCode().setMsg("删除Work成功!");
	}
	
	}
